import 'package:flutter/material.dart';

import 'package:heven/widget/input.dart';
import 'package:heven/pages/creation/widget/label.dart';
import 'package:heven/widget/button.dart';
import 'package:heven/api/index.dart';
import 'package:heven/utils/index.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:heven/widget/toast.dart';

class BaseWidget extends StatefulWidget {
  const BaseWidget({super.key, this.dialog});
  final Function(BuildContext)? dialog;
  @override
  State<BaseWidget> createState() => _BaseWidgetState();
}

class _BaseWidgetState extends State<BaseWidget> {
  bool _switchValue = false;
  String _prompt = '';
  bool _loading = false;
  late FToast fToast;
  // KeyboardUtils _keyboardUtils = KeyboardUtils();
  late int tempId;
  void initState() {
    super.initState();

    fToast = FToast();
    // if you want to use context from globally instead of content we need to pass navigatorKey.currentContext!
    fToast.init(context);
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final double bottomPadding = MediaQuery.of(context).padding.bottom + 10;

    return GestureDetector(
      onTap: () {
        print(11);
        FocusScope.of(context).unfocus();
      },
      child: Column(
        children: [
          Expanded(
            flex: 1,
            child: Scrollbar(
              child: SingleChildScrollView(
                child: Container(
                  padding: const EdgeInsets.fromLTRB(23, 4, 18, 0),
                  child: Column(
                    children: [
                      LabelWidget(
                        name: '灵感描述',
                        showRight: true,
                        onChanged: (value) {
                          setState(() {
                            _switchValue = value;
                          });
                        },
                      ),
                      InputWidget(
                        maxLength: 200,
                        maxLines: null,
                        showCount: true,
                        textInputAction: TextInputAction.newline,
                        height: 200,
                        hintText: !_switchValue
                            ? '输入你的音乐创作灵感，点击“开始创作” 例如：写一首抒情歌曲，流行曲风，浪漫，星空，要求是中文。'
                            : '摇滚乐，最浪漫的摇滚',
                        onChanged: (value) {
                          _prompt = value;
                        },
                      ),
                      // TextField(
                      //   onChanged: (value) {
                      //     setState(() {
                      //       _prompt = value;
                      //     });
                      //   },
                      // ),
                      SizedBox(
                        height: bottomPadding,
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(0, 10, 0, bottomPadding),
            child: ButtonWidget(
              child: const Text(
                '开始创作',
                style: TextStyle(
                  fontSize: 15,
                  color: Color(0xFF000000),
                  fontWeight: FontWeight.bold,
                ),
              ),
              onTap: () async {
                if (_prompt.isEmpty) {
                  fToast.showToast(
                    child: ToastWidget(text: '请输入创作灵感!'),
                    gravity: ToastGravity.CENTER,
                  );
                  return;
                }

                var uid = await Common.getStorageValue('uid');
                var token = await Common.getStorageValue('token');
                var isVip = await Common.getStorageValue('isVip');

                if (token == null) {
                  Navigator.pushNamed(context, '/login/list');
                  return;
                }
                if (_loading) {
                  return;
                }
                _loading = true;
                tomusic({
                  "prompt": _prompt,
                  "type": "basic",
                  "uid": uid,
                  "token": token
                }).then((value) {
                  print(value);
                  if (value['status'] == 200) {
                    Navigator.pushNamedAndRemoveUntil(
                        context,
                        '/home', // 你的首页路由名称
                        (route) => false, // 移除所有页面
                        arguments: {"index": 1});
                  } else if (value['status'] == 406) {
                    FocusManager.instance.primaryFocus?.unfocus();
                    // _showDialog(context);
                    widget.dialog!(context);
                  } else {
                    if (isVip != null && isVip == "true") {
                      fToast.showToast(
                        child: ToastWidget(text: value['message']),
                        gravity: ToastGravity.CENTER,
                      );
                      return;
                    }
                    fToast.showToast(
                      child: ToastWidget(text: value['message']),
                      gravity: ToastGravity.CENTER,
                    );
                    // FocusScope.of(context).unfocus();
                  }
                  _loading = false;
                }).catchError(() {
                  _loading = false;
                });
                //
              },
            ),
          ),
        ],
      ),
    );
  }
}
